#ifndef _YUV_H
#define _YUV_H

#include <windows.h>
#include "stdafx.h"

#define YUV422	422
#define YUV420	420

class CYUVTable
{
public :
	// Static methods
	static BYTE GetY(const BYTE p_nR, const BYTE p_nG, const BYTE p_nB)
	{
		int y = (int)(0.299 * p_nR + 0.587 * p_nG + 0.114 * p_nB);
		return max(0, min(y, 255));
	};
	static BYTE GetU(const BYTE p_nR, const BYTE p_nG, const BYTE p_nB)
	{
		int u = (int)(-0.169 * p_nR - 0.311 * p_nG + 0.500 * p_nB) + 128;
		return max(0, min(u, 255));
	};
	static BYTE GetV(const BYTE p_nR, const BYTE p_nG, const BYTE p_nB)
	{
		int v = (int)(0.500 * p_nR - 0.419 * p_nG - 0.081 * p_nB) + 128;
		return max(0, min(v, 255));
	};

//		pbRGB[i*3] = Y+0.956U+0.621V
//		pbRGB[i*3+1] = Y+0.272U+0.647V
//		pbRGB[i*3+2] = Y+1.1061U+1.703V

	static BYTE GetR(const int p_nY, const int p_nU, const int p_nV)
	{
		int r = (int)(p_nY - 0.001 * (p_nU - 128) + 1.402 * (p_nV - 128));
		return max(0, min(r, 255));
	};
	static BYTE GetG(const int p_nY, const int p_nU, const int p_nV)
	{
		int g = (int)(p_nY - 0.344 * (p_nU - 128) - 0.714 * (p_nV - 128));
		return max(0, min(g, 255));
	};
	static BYTE GetB(const int p_nY, const int p_nU, const int p_nV)
	{
		int b = (int)(p_nY + 1.772 * (p_nU - 128) + 0.001 * (p_nV - 128));
		return max(0, min(b, 255));
	};
};


class CYUVtoRGB24 {

	int m_width, m_height;

	int m_yuv_len;
	int m_rgb_len;
	int m_yuv_type;

	unsigned char *m_rgb_buff;
	unsigned char *m_yuv_buff;

private :

	void YUV422UYVY_ToRGB24(unsigned char *m_pbYUV, unsigned char *m_pbRGB, int m_yuv_len);
	void YUV422YUY2_ToRGB24(unsigned char *m_pbYUV, unsigned char *m_pbRGB, int m_yuv_len);
	void YUV420ToRGB24(unsigned char *m_pbYUV, unsigned char *m_pbRGB, int m_width, int m_height);
	void YUV422UYVY_ToRGB24(BYTE *pbYUV, BYTE *pbRGB, int width, int height, int Stride);

public :

	CYUVtoRGB24();
	~CYUVtoRGB24();

	// copy image data to yuv_buff
	int SetYuvImage(unsigned char *buff, int len, int width, int height, int type);

	// Conversion yuv data to RGB32 as defined yuv_type
	int YUVtoRGB(void);

	// return rgb image data
	unsigned char* GetRgbImage(void);

}; // class C_YUVtoRGB32

#endif